package br.com.commons.utility.validator;

import java.util.ArrayList;
import java.util.List;

import br.com.commons.utility.exception.AppValidatorException;
import br.com.commons.utility.message.Message;
import br.com.commons.utility.message.TypeMessage;

public abstract class Validator {
	
	private List<Message> messages = new ArrayList<Message>();
	
	public void execute(Object object) throws AppValidatorException {
		for (Validation validation : validate(object)) {
			if (!validation.getValidationRule().applyRule(validation.getField())) {
				messages.add(new Message(TypeMessage.ERROR, validation.getMessage()));
		    }
		}
		if(!messages.isEmpty()) { 
			throw new AppValidatorException(messages); 
		}
	}

	protected abstract List<Validation> validate(Object object);
	
}
